<!doctype html>
<html lang="en" class="no-js">

  <!-- Mirrored from flatbuffers.dev/languages/c_sharp/ by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 08 May 2025 07:01:35 GMT -->
  <!-- Added by HTTrack --><meta http-equiv="content-type"
    content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">

    <link rel="canonical" href="index.html">

    <link rel="prev" href="../cpp/index.html">

    <link rel="next" href="../dart/index.html">

    <link rel="icon" href="../../assets/images/favicon.png">
    <meta name="generator" content="mkdocs-1.6.1, mkdocs-material-9.6.9">

    <title>C# - FlatBuffers Docs （文档）</title>

    <link rel="stylesheet"
      href="../../assets/stylesheets/main.4af4bdda.min.css">

    <link rel="stylesheet"
      href="../../assets/stylesheets/palette.06af60db.min.css">

    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link rel="stylesheet"
      href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&amp;display=fallback">
    <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>

    <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>

  </head>

  <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="indigo"
    data-md-color-accent="indigo">

    <input class="md-toggle" data-md-toggle="drawer" type="checkbox"
      id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox"
      id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">

      <a href="#flatbuffers_guide_use_c-sharp" class="md-skip">
        跳到内容 (Skip to content)
      </a>

    </div>
    <div data-md-component="announce">

    </div>

    <header class="md-header md-header--shadow" data-md-component="header">
      <nav class="md-header__inner md-grid" aria-label="Header">
        <a href="../../index.html" title="FlatBuffers Docs （文档）"
          class="md-header__button md-logo" aria-label="FlatBuffers Docs （文档）"
          data-md-component="logo">

          <img src="../../assets/flatbuffers_logo.svg" alt="logo">

        </a>
        <label class="md-header__button md-icon" for="__drawer">

          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
              d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z" /></svg>
        </label>
        <div class="md-header__title" data-md-component="header-title">
          <div class="md-header__ellipsis">
            <div class="md-header__topic">
              <span class="md-ellipsis">
                FlatBuffers Docs （文档）
              </span>
            </div>
            <div class="md-header__topic" data-md-component="header-topic">
              <span class="md-ellipsis">

                C#

              </span>
            </div>
          </div>
        </div>

        <form class="md-header__option" data-md-component="palette">

          <input class="md-option" data-md-color-media
            data-md-color-scheme="default" data-md-color-primary="indigo"
            data-md-color-accent="indigo" aria-label="Switch to dark mode"
            type="radio" name="__palette" id="__palette_0">

          <label class="md-header__button md-icon" title="Switch to dark mode"
            for="__palette_1" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z" /></svg>
          </label>

          <input class="md-option" data-md-color-media
            data-md-color-scheme="slate" data-md-color-primary="indigo"
            data-md-color-accent="indigo" aria-label="Switch to light mode"
            type="radio" name="__palette" id="__palette_1">

          <label class="md-header__button md-icon" title="Switch to light mode"
            for="__palette_0" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z" /></svg>
          </label>

        </form>

        <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script>

        <div class="md-header__source">
          <a href="https://github.com/google/flatbuffers"
            title="Go to repository" class="md-source"
            data-md-component="source">
            <div class="md-source__icon md-icon">

              <svg xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                  d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2" /></svg>
            </div>
            <div class="md-source__repository">
              google/FlatBuffers
            </div>
          </a>
        </div>

      </nav>

    </header>

    <div class="md-container" data-md-component="container">

      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">

          <div class="md-sidebar md-sidebar--primary"
            data-md-component="sidebar" data-md-type="navigation">
            <div class="md-sidebar__scrollwrap">
              <div class="md-sidebar__inner">

                <nav class="md-nav md-nav--primary" aria-label="Navigation"
                  data-md-level="0">
                  <label class="md-nav__title" for="__drawer">
                    <a href="../../index.html" title="FlatBuffers Docs （文档）"
                      class="md-nav__button md-logo"
                      aria-label="FlatBuffers Docs （文档）"
                      data-md-component="logo">

                      <img src="../../assets/flatbuffers_logo.svg" alt="logo">

                    </a>
                    FlatBuffers Docs （文档）
                  </label>

                  <div class="md-nav__source">
                    <a href="https://github.com/google/flatbuffers"
                      title="Go to repository" class="md-source"
                      data-md-component="source">
                      <div class="md-source__icon md-icon">

                        <svg xmlns="http://www.w3.org/2000/svg"
                          viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                            d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2" /></svg>
                      </div>
                      <div class="md-source__repository">
                        google/FlatBuffers
                      </div>
                    </a>
                  </div>

                  <ul class="md-nav__list" data-md-scrollfix>

                    <li class="md-nav__item">
                      <a href="../../index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          概述 (Overview)

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../../quick_start/index.html"
                        class="md-nav__link">

                        <span class="md-ellipsis">
                          快速入门 (Quick Start)

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../../tutorial/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          教程 (Tutorial)

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_4">

                      <label class="md-nav__link" for="__nav_4"
                        id="__nav_4_label" tabindex="0">

                        <span class="md-ellipsis">
                          编译器 (flatc) (Compiler (flatc))

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_4_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_4">
                          <span class="md-nav__icon md-icon"></span>
                          编译器 (flatc) (Compiler (flatc))
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../../building/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                构建 (Building)

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../../flatc/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                使用 (Using)

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_5">

                      <label class="md-nav__link" for="__nav_5"
                        id="__nav_5_label" tabindex="0">

                        <span class="md-ellipsis">
                          模式定义 (.fbs) (Schema (.fbs))

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_5_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_5">
                          <span class="md-nav__icon md-icon"></span>
                          模式定义 (.fbs) (Schema (.fbs))
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../../schema/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                概述 (Overview)

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../../evolution/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                演变 (Evolution)

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../../grammar/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                语法 (Grammar)

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li
                      class="md-nav__item md-nav__item--active md-nav__item--nested">

                      <input class="md-nav__toggle md-toggle " type="checkbox"
                        id="__nav_6" checked>

                      <label class="md-nav__link" for="__nav_6"
                        id="__nav_6_label" tabindex="0">

                        <span class="md-ellipsis">
                          语言指南 (Language Guides)

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_6_label" aria-expanded="true">
                        <label class="md-nav__title" for="__nav_6">
                          <span class="md-nav__icon md-icon"></span>
                          语言指南 (Language Guides)
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../c/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                C

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../cpp/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                C++

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item md-nav__item--active">

                            <input class="md-nav__toggle md-toggle"
                              type="checkbox" id="__toc">

                            <label class="md-nav__link md-nav__link--active"
                              for="__toc">

                              <span class="md-ellipsis">
                                C#

                              </span>

                              <span class="md-nav__icon md-icon"></span>
                            </label>

                            <a href="index.html"
                              class="md-nav__link md-nav__link--active">

                              <span class="md-ellipsis">
                                C#

                              </span>

                            </a>

                            <nav class="md-nav md-nav--secondary"
                              aria-label="Table of contents">

                              <label class="md-nav__title" for="__toc">
                                <span class="md-nav__icon md-icon"></span>
                                目录 (Table of contents)
                              </label>
                              <ul class="md-nav__list" data-md-component="toc"
                                data-md-scrollfix>

                                <li class="md-nav__item">
                                  <a href="#before-you-get-started"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      开始之前 (Before you get started)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#flatbuffers-c-code-location"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      FlatBuffers C#代码位置 (FlatBuffers C# code
                                      location)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#building-the-flatbuffers-c-library"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      构建FlatBuffers C#库 (Building the
                                      FlatBuffers C# library)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#testing-the-flatbuffers-c-library"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      测试FlatBuffers C#库 (Testing the FlatBuffers
                                      C# library)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#using-the-flatbuffers-c-library"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      使用FlatBuffers C#库 (Using the FlatBuffers
                                      C# library)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a
                                    href="#storing-dictionaries-in-a-flatbuffer"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      在FlatBuffer中存储字典 (Storing dictionaries in
                                      a FlatBuffer)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#buffer-verification"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      缓冲区验证 (Buffer verification)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#text-parsing" class="md-nav__link">
                                    <span class="md-ellipsis">
                                      文本解析 (Text parsing)
                                    </span>
                                  </a>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#object-based-api"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      基于对象的API (Object based API)
                                    </span>
                                  </a>

                                  <nav class="md-nav"
                                    aria-label="Object based API">
                                    <ul class="md-nav__list">

                                      <li class="md-nav__item">
                                        <a href="#json-serialization"
                                          class="md-nav__link">
                                          <span class="md-ellipsis">
                                            JSON序列化 (Json Serialization)
                                          </span>
                                        </a>

                                      </li>

                                    </ul>
                                  </nav>

                                </li>

                                <li class="md-nav__item">
                                  <a href="#conditional-compilation-symbols"
                                    class="md-nav__link">
                                    <span class="md-ellipsis">
                                      条件编译符号 (Conditional compilation symbols)
                                    </span>
                                  </a>

                                </li>

                              </ul>

                            </nav>

                          </li>

                          <li class="md-nav__item">
                            <a href="../dart/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Dart

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../go/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Go

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../java/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Java

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../javascript/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                JavasScript

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../kotlin/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Kotlin

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../lobster/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                Lobster

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../lua/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Lua

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../php/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                PHP

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../python/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Python

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../rust/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Rust

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../swift/index.html" class="md-nav__link">

                              <span class="md-ellipsis">
                                Swift

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../typescript/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                TypeScript

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="../../support/index.html" class="md-nav__link">

                        <span class="md-ellipsis">
                          支持的配置 (Supported Configurations)

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../../white_paper/index.html"
                        class="md-nav__link">

                        <span class="md-ellipsis">
                          白皮书 (White Paper)

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item md-nav__item--nested">

                      <input
                        class="md-nav__toggle md-toggle md-toggle--indeterminate"
                        type="checkbox" id="__nav_9">

                      <label class="md-nav__link" for="__nav_9"
                        id="__nav_9_label" tabindex="0">

                        <span class="md-ellipsis">
                          高级 (Advanced)

                        </span>

                        <span class="md-nav__icon md-icon"></span>
                      </label>

                      <nav class="md-nav" data-md-level="1"
                        aria-labelledby="__nav_9_label" aria-expanded="false">
                        <label class="md-nav__title" for="__nav_9">
                          <span class="md-nav__icon md-icon"></span>
                          高级 (Advanced)
                        </label>
                        <ul class="md-nav__list" data-md-scrollfix>

                          <li class="md-nav__item">
                            <a href="../../internals/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                FlatBuffers内部结构 (FlatBuffers Internals)

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a
                              href="../../intermediate_representation/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                中间表示 (Intermediate Representation)

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../../annotation/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                注释缓冲区 (.afb) (Annotating Buffers (.afb))

                              </span>

                            </a>
                          </li>

                          <li class="md-nav__item">
                            <a href="../../benchmarks/index.html"
                              class="md-nav__link">

                              <span class="md-ellipsis">
                                基准测试 (Benchmarks)

                              </span>

                            </a>
                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="../../flexbuffers/index.html"
                        class="md-nav__link">

                        <span class="md-ellipsis">
                          FlexBuffers (无模式版本) (Schema-less version)

                        </span>

                      </a>
                    </li>

                    <li class="md-nav__item">
                      <a href="../../contributing/index.html"
                        class="md-nav__link">

                        <span class="md-ellipsis">
                          贡献 (Contributing)

                        </span>

                      </a>
                    </li>

                  </ul>
                </nav>
              </div>
            </div>
          </div>

          <div class="md-sidebar md-sidebar--secondary"
            data-md-component="sidebar" data-md-type="toc">
            <div class="md-sidebar__scrollwrap">
              <div class="md-sidebar__inner">

                <nav class="md-nav md-nav--secondary"
                  aria-label="Table of contents">

                  <label class="md-nav__title" for="__toc">
                    <span class="md-nav__icon md-icon"></span>
                    目录 (Table of contents)
                  </label>
                  <ul class="md-nav__list" data-md-component="toc"
                    data-md-scrollfix>

                    <li class="md-nav__item">
                      <a href="#before-you-get-started" class="md-nav__link">
                        <span class="md-ellipsis">
                          开始之前 (Before you get started)
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#flatbuffers-c-code-location"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          FlatBuffers C#代码位置 (FlatBuffers C# code location)
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#building-the-flatbuffers-c-library"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          构建FlatBuffers C#库 (Building the FlatBuffers C#
                          library)
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#testing-the-flatbuffers-c-library"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          测试FlatBuffers C#库 (Testing the FlatBuffers C# library)
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#using-the-flatbuffers-c-library"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          使用FlatBuffers C#库 (Using the FlatBuffers C# library)
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#storing-dictionaries-in-a-flatbuffer"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          Storing dictionaries in a FlatBuffer
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#buffer-verification" class="md-nav__link">
                        <span class="md-ellipsis">
                          Buffer verification
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#text-parsing" class="md-nav__link">
                        <span class="md-ellipsis">
                          Text parsing
                        </span>
                      </a>

                    </li>

                    <li class="md-nav__item">
                      <a href="#object-based-api" class="md-nav__link">
                        <span class="md-ellipsis">
                          Object based API
                        </span>
                      </a>

                      <nav class="md-nav" aria-label="Object based API">
                        <ul class="md-nav__list">

                          <li class="md-nav__item">
                            <a href="#json-serialization" class="md-nav__link">
                              <span class="md-ellipsis">
                                Json Serialization
                              </span>
                            </a>

                          </li>

                        </ul>
                      </nav>

                    </li>

                    <li class="md-nav__item">
                      <a href="#conditional-compilation-symbols"
                        class="md-nav__link">
                        <span class="md-ellipsis">
                          Conditional compilation symbols
                        </span>
                      </a>

                    </li>

                  </ul>

                </nav>
              </div>
            </div>
          </div>

          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">

              <a
                href="https://github.com/google/flatbuffers/edit/master/docs/source/languages/c_sharp.md"
                title="编辑此页面 (Edit this page)"
                class="md-content__button md-icon">

                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 24 24"><path
                    d="M10 20H6V4h7v5h5v3.1l2-2V8l-6-6H6c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h4zm10.2-7c.1 0 .3.1.4.2l1.3 1.3c.2.2.2.6 0 .8l-1 1-2.1-2.1 1-1c.1-.1.2-.2.4-.2m0 3.9L14.1 23H12v-2.1l6.1-6.1z" /></svg>
              </a>

              <h1 id="flatbuffers_guide_use_c-sharp">在C#中使用 (Use in C#)</h1>
              <h2 id="before-you-get-started">开始之前 (Before you get started)</h2>
              <p>在深入了解C#中的FlatBuffers使用之前，应该注意
                <a href="../../tutorial/index.html">教程 (Tutorial)</a>页面有一个完整的指南，
                介绍了所有支持语言（包括C#）中FlatBuffers的一般使用方法。
                本页面旨在介绍FlatBuffers使用的细微差别，特别是针对C#的部分。</p>
              <p>您还应该阅读<a
                  href="../../building/index.html">构建 (Building)</a>
                文档以构建<code>flatc</code>，并且应该熟悉
                <a href="../../flatc/index.html">使用模式编译器 (Using the schema
                  compiler)</a>
                和<a href="../../schema/index.html">编写模式 (Writing a
                  schema)</a>。</p>
              <h2 id="flatbuffers-c-code-location">FlatBuffers C#代码位置
                (FlatBuffers C# code location)</h2>
              <p>FlatBuffers C#库的代码可以在
                <code>flatbuffers/net/FlatBuffers</code>找到。您可以在
                <a
                  href="https://github.com/google/flatbuffers/tree/master/net/FlatBuffers">FlatBuffers
                  GitHub页面</a>浏览该库。</p>
              <h2 id="building-the-flatbuffers-c-library">构建FlatBuffers C#库
                (Building the FlatBuffers C# library)</h2>
              <p><code>FlatBuffers.csproj</code>项目包含.NET Standard 2.1、
                .NET 6和.NET 8的多目标支持。</p>
              <p>使用跨平台的
                <a href="https://dotnet.microsoft.com/download">.NET Core
                  SDK</a>时，可以通过添加<code>-f</code>
                命令行选项为特定框架目标构建：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span>dotnet<span class="w"> </span>build<span class="w"> </span>-f<span class="w"> </span>netstandard2.1<span class="w"> </span><span class="s2">&quot;FlatBuffers.csproj&quot;</span>
</code></pre></div>
              <p><code>FlatBuffers.csproj</code>项目还支持使用
                <code>-p</code>命令行选项定义各种条件编译符号
                （请参阅下面的"条件编译符号"部分）：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span>dotnet<span class="w"> </span>build<span class="w"> </span>-f<span class="w"> </span>netstandard2.1<span class="w"> </span>-p:ENABLE_SPAN_T<span class="o">=</span><span class="nb">true</span><span class="w"> </span>-p:UNSAFE_BYTEBUFFER<span class="o">=</span><span class="nb">true</span><span class="w"> </span><span class="s2">&quot;FlatBuffers.csproj&quot;</span>
</code></pre></div>
              <h2 id="testing-the-flatbuffers-c-library">测试FlatBuffers C#库
                (Testing the FlatBuffers C# library)</h2>
              <p>测试库的代码可以在
                <code>flatbuffers/tests</code>找到。</p>
              <p>C#的测试代码位于<a
                  href="https://github.com/google/flatbuffers/tree/master/tests/FlatBuffers.Test">FlatBuffers.Test</a>
                子文件夹中。要运行测试，请在<a
                  href="https://www.visualstudio.com/">Visual
                  Studio</a>中打开<code>FlatBuffers.Test.csproj</code>，
                然后编译/运行该项目。</p>
              <p>您也可以使用<a
                  href="http://www.mono-project.com/">Mono</a>来运行测试。
                安装Mono后，您可以通过在
                <code>FlatBuffers.Test</code>文件夹内运行以下命令来从命令行运行测试：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span>mcs<span class="w"> </span>*.cs<span class="w"> </span>../MyGame/Example/*.cs<span class="w"> </span>../../net/FlatBuffers/*.cs
<span class="w">    </span>mono<span class="w"> </span>Assert.exe
</code></pre></div>
              <h2 id="using-the-flatbuffers-c-library">使用FlatBuffers C#库 (Using
                the FlatBuffers C# library)</h2>
              <p><em>注意：有关如何在C#中使用FlatBuffers的更深入示例，请参阅<a
                    href="../../tutorial/index.html">教程 (Tutorial)</a>。</em></p>
              <p>FlatBuffers支持在C#中读取和写入二进制FlatBuffers。</p>
              <p>要在您自己的代码中使用FlatBuffers，首先使用<code>flatc</code>的<code>--csharp</code>选项
                从您的模式生成C#类。然后，您可以包含FlatBuffers和生成的代码来读取
                或写入FlatBuffer。</p>
              <p>例如，以下是如何在C#中读取FlatBuffer二进制文件：
                首先，导入库和生成的代码。然后，将FlatBuffer二进制文件读入
                <code>byte[]</code>。随后将<code>byte[]</code>转换为<code>ByteBuffer</code>，
                并将其传递给<code>GetRootAsMyRootType</code>函数：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span><span class="k">using</span><span class="w"> </span><span class="nn">MyGame.Example</span><span class="p">;</span>
<span class="w">    </span><span class="k">using</span><span class="w"> </span><span class="nn">Google.FlatBuffers</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 此代码片段为简洁起见忽略了异常处理。</span>
<span class="w">    </span><span class="kt">byte</span><span class="p">[]</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">File</span><span class="p">.</span><span class="n">ReadAllBytes</span><span class="p">(</span><span class="s">&quot;monsterdata_test.mon&quot;</span><span class="p">);</span>

<span class="w">    </span><span class="n">ByteBuffer</span><span class="w"> </span><span class="n">bb</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ByteBuffer</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
<span class="w">    </span><span class="n">Monster</span><span class="w"> </span><span class="n">monster</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">GetRootAsMonster</span><span class="p">(</span><span class="n">bb</span><span class="p">);</span>
</code></pre></div>
              <p>现在可以从<code>Monster monster</code>访问数据：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span><span class="kt">short</span><span class="w"> </span><span class="n">hp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Hp</span><span class="p">;</span>
<span class="w">    </span><span class="n">Vec3</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Pos</span><span class="p">;</span>
</code></pre></div>
              <p>C#代码命名遵循标准C#风格，使用PascalCasing标识符，
                例如<code>GetRootAsMyRootType</code>。此外，值（除了向量和联合）
                作为属性而不是无参数访问器方法提供。
                可以传递已创建对象的性能增强方法以<code>Get</code>为前缀，例如：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span><span class="c1">// 属性</span>
<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">monster</span><span class="p">.</span><span class="n">Pos</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 填充预构造对象的方法</span>
<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">preconstructedPos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Vec3</span><span class="p">();</span>
<span class="w">    </span><span class="n">monster</span><span class="p">.</span><span class="n">GetPos</span><span class="p">(</span><span class="n">preconstructedPos</span><span class="p">);</span>
</code></pre></div>
              <h2 id="storing-dictionaries-in-a-flatbuffer">在FlatBuffer中存储字典
                (Storing dictionaries in a FlatBuffer)</h2>
              <p>FlatBuffers本身不支持字典，但有支持用向量和二分查找来模拟它们的行为，
                这意味着您可以直接从FlatBuffer进行快速查找，而无需将数据解包到
                <code>Dictionary</code>或类似结构中。</p>
              <p>使用方法：
                - 将表中的一个字段指定为"键"字段。通过在该字段上设置<code>key</code>属性来实现，
                例如<code>name:string (key)</code>。
                您只能有一个键字段，它必须是字符串或标量类型。
                - 像往常一样写出此类型的表，将它们的偏移量收集在数组中。
                -
                不要调用标准生成的方法，例如：<code>Monster.createTestarrayoftablesVector</code>，
                而是在C#中调用<code>CreateSortedVectorOfMonster</code>，
                它会首先对所有偏移量进行排序，使得它们引用的表按键字段排序，然后序列化它。
                - 现在当您访问FlatBuffer时，可以使用<code>ByKey</code>访问器来访问向量的元素，例如：
                在C#中使用<code>monster.TestarrayoftablesByKey("Frodo")</code>，
                它返回相应表类型的对象，如果未找到则返回<code>null</code>。
                <code>ByKey</code>执行二分查找，因此应该有类似于<code>Dictionary</code>的速度，
                但由于更好的缓存可能会更快。
                <code>ByKey</code>只有在向量已排序的情况下才能工作，如果未排序，
                可能找不到元素。</p>
              <h2 id="buffer-verification">缓冲区验证 (Buffer verification)</h2>
              <p>如<a href="../cpp/index.html">C++使用 (C++ Usage)</a>中所述，
                缓冲区访问器函数不会在运行时验证缓冲区偏移量。
                如有必要，您可以在访问数据之前选择使用缓冲区验证器。
                此验证器将检查所有偏移量、所有字段的大小和字符串的空终止，
                以确保当访问缓冲区时，所有读取都将在缓冲区内部结束。</p>
              <p>每个根类型都将生成一个验证函数，
                例如<code>Monster.VerifyMonster</code>。可以按如下方式调用：
                <div
                  class="highlight"><pre><span></span><code><span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">ok</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Monster</span><span class="p">.</span><span class="n">VerifyMonster</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
</code></pre></div>
                如果<code>ok</code>为true，则缓冲区可以安全读取。</p>
              <p>对于更详细的验证控制，可以使用<code>Monster</code>类型的
                <code>MonsterVerify.Verify</code>：
                <div
                  class="highlight"><pre><span></span><code><span class="w">    </span><span class="err">#</span><span class="w"> </span><span class="n">调用序列</span>
<span class="w">    </span><span class="n">FlatBuffers</span><span class="p">.</span><span class="n">Verifier</span><span class="w"> </span><span class="n">verifier</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBuffers</span><span class="p">.</span><span class="n">Verifier</span><span class="p">(</span><span class="n">buf</span><span class="p">);</span>
<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">ok</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">verifier</span><span class="p">.</span><span class="n">VerifyBuffer</span><span class="p">(</span><span class="s">&quot;MONS&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">false</span><span class="p">,</span><span class="w"> </span><span class="n">MonsterVerify</span><span class="p">.</span><span class="n">Verify</span><span class="p">);</span>

<span class="w">    </span><span class="err">#</span><span class="w"> </span><span class="n">或单行调用</span>
<span class="w">    </span><span class="kt">var</span><span class="w"> </span><span class="n">ok</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBuffers</span><span class="p">.</span><span class="n">Verifier</span><span class="p">(</span><span class="n">bb</span><span class="p">).</span><span class="n">setStringCheck</span><span class="p">(</span><span class="k">true</span><span class="p">).</span>\
<span class="w">             </span><span class="n">VerifyBuffer</span><span class="p">(</span><span class="s">&quot;MONS&quot;</span><span class="p">,</span><span class="w"> </span><span class="k">false</span><span class="p">,</span><span class="w"> </span><span class="n">MonsterVerify</span><span class="p">.</span><span class="n">Verify</span><span class="p">);</span>
</code></pre></div>
                如果<code>ok</code>为true，则缓冲区可以安全读取。</p>
              <p><code>verifyBuffer</code>的第二个参数指定缓冲区内容是否
                包含大小前缀。在上面的示例中，假定缓冲区不包括
                大小前缀（<code>false</code>）。</p>
              <p>验证器支持可以使用适当的流畅方法设置的选项：
                * SetMaxDepth - 限制嵌套深度。默认值：1000000
                * SetMaxTables - 验证器可能遇到的表的总数。默认值：64
                * SetAlignmentCheck - 检查内容对齐。默认值：True
                * SetStringCheck - 检查字符串是否包含终止'0'字符。默认值：true</p>
              <h2 id="text-parsing">文本解析 (Text parsing)</h2>
              <p>目前没有支持直接从C#解析文本（模式和JSON），
                虽然您可以通过每种语言可用的本地调用接口使用C++解析器。
                有关文本解析的更多信息，请参阅C++文档。</p>
              <h2 id="object-based-api">基于对象的API (Object based API)</h2>
              <p>FlatBuffers的重点是内存效率，这就是为什么它的基本API
                围绕尽可能少地使用内存而设计。这确实使API更加笨拙
                （需要预先构建所有数据，并使变更更加困难）。</p>
              <p>对于效率不那么重要的情况，可以使用更方便的基于对象的API
                （通过<code>--gen-object-api</code>），它能够将FlatBuffer解包并打包为对象
                和标准<code>System.Collections.Generic</code>容器，
                从而实现便捷的构建、访问和变更。</p>
              <p>使用方法：</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span><span class="c1">// 从缓冲区反序列化到对象。</span>
<span class="w">    </span><span class="n">MonsterT</span><span class="w"> </span><span class="n">monsterobj</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">GetMonster</span><span class="p">(</span><span class="n">flatbuffer</span><span class="p">).</span><span class="n">UnPack</span><span class="p">();</span>

<span class="w">    </span><span class="c1">// 直接像C#类实例一样更新对象。</span>
<span class="w">    </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">monsterobj</span><span class="p">.</span><span class="n">Name</span><span class="p">);</span>
<span class="w">    </span><span class="n">monsterobj</span><span class="p">.</span><span class="n">Name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;Bob&quot;</span><span class="p">;</span><span class="w">  </span><span class="c1">// 更改名称。</span>

<span class="w">    </span><span class="c1">// 序列化到新的flatbuffer。</span>
<span class="w">    </span><span class="n">FlatBufferBuilder</span><span class="w"> </span><span class="n">fbb</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FlatBufferBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="w">    </span><span class="n">fbb</span><span class="p">.</span><span class="n">Finish</span><span class="p">(</span><span class="n">Monster</span><span class="p">.</span><span class="n">Pack</span><span class="p">(</span><span class="n">fbb</span><span class="p">,</span><span class="w"> </span><span class="n">monsterobj</span><span class="p">).</span><span class="n">Value</span><span class="p">);</span>
</code></pre></div>
              <h3 id="json-serialization">JSON序列化 (Json Serialization)</h3>
              <p>对象API的另一个功能是允许您
                序列化和反序列化JSON文本。
                要使用JSON序列化，请向<code>flatc</code>添加
                <code>--cs-gen-json-serializer</code>选项，并
                将<code>Newtonsoft.Json</code> nuget包添加到csproj中。这同时需要
                明确设置<code>--gen-object-api</code>选项。</p>
              <div
                class="highlight"><pre><span></span><code><span class="w">    </span><span class="c1">// 从json反序列化MonsterT</span>
<span class="w">    </span><span class="kt">string</span><span class="w"> </span><span class="n">jsonText</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">File</span><span class="p">.</span><span class="n">ReadAllText</span><span class="p">(</span><span class="s">@&quot;Resources/monsterdata_test.json&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">MonsterT</span><span class="w"> </span><span class="n">mon</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MonsterT</span><span class="p">.</span><span class="n">DeserializeFromJson</span><span class="p">(</span><span class="n">jsonText</span><span class="p">);</span>

<span class="w">    </span><span class="c1">// 将MonsterT序列化为json</span>
<span class="w">    </span><span class="kt">string</span><span class="w"> </span><span class="n">jsonText2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mon</span><span class="p">.</span><span class="n">SerializeToJson</span><span class="p">();</span>
</code></pre></div>
              <ul>
                <li>限制</li>
                <li>目前不支持<code>hash</code>属性。</li>
                <li>NuGet包依赖</li>
                <li><a
                    href="https://github.com/JamesNK/Newtonsoft.Json">Newtonsoft.Json</a></li>
              </ul>
              <h2 id="conditional-compilation-symbols">条件编译符号 (Conditional
                compilation symbols)</h2>
              <p>有三个条件编译符号会影响C# <code>ByteBuffer</code>实现的
                性能/功能。</p>
              <ul>
                <li><code>UNSAFE_BYTEBUFFER</code></li>
              </ul>
              <p>这将使用不安全代码来操作底层字节数组。这可以
                产生合理的性能提升。</p>
              <ul>
                <li><code>BYTEBUFFER_NO_BOUNDS_CHECK</code></li>
              </ul>
              <p>这将禁用对字节数组的边界检查断言。这可以在
                正常代码中获得小幅性能提升。</p>
              <ul>
                <li><code>ENABLE_SPAN_T</code></li>
              </ul>
              <p>这将启用使用<code>Span&lt;T&gt;</code>而不仅仅是<code>T[]</code>来读写内存块。
                您还可以通过提供<code>ByteBufferAllocator</code>的自定义实现，
                启用直接写入共享内存或其他类型的内存。
                <code>ENABLE_SPAN_T</code>还需要定义<code>UNSAFE_BYTEBUFFER</code>，
                或者使用.NET Standard 2.1。</p>
              <p>同时使用<code>UNSAFE_BYTEBUFFER</code>和
                <code>BYTEBUFFER_NO_BOUNDS_CHECK</code>可以为某些操作带来约15%的性能提升，
                但这样做可能存在风险。使用时请自行承担风险！</p>
              <p><br></p>

            </article>
          </div>

          <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

          <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script>
        </div>

      </main>

      <footer class="md-footer">

        <nav class="md-footer__inner md-grid" aria-label="Footer">

          <a href="../cpp/index.html"
            class="md-footer__link md-footer__link--prev"
            aria-label="Previous: C++">
            <div class="md-footer__button md-icon">

              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                  d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11z" /></svg>
            </div>
            <div class="md-footer__title">
              <span class="md-footer__direction">
                上一页 (Previous)
              </span>
              <div class="md-ellipsis">
                C++
              </div>
            </div>
          </a>

          <a href="../dart/index.html"
            class="md-footer__link md-footer__link--next"
            aria-label="Next: Dart">
            <div class="md-footer__title">
              <span class="md-footer__direction">
                下一页 (Next)
              </span>
              <div class="md-ellipsis">
                Dart
              </div>
            </div>
            <div class="md-footer__button md-icon">

              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path
                  d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11z" /></svg>
            </div>
          </a>

        </nav>

        <div class="md-footer-meta md-typeset">
          <div class="md-footer-meta__inner md-grid">
            <div class="md-copyright">

              <div class="md-copyright__highlight">
                版权所有 &copy; 2025 Google (Copyright &copy; 2025 Google)
              </div>

              使用
              <a href="https://squidfunk.github.io/mkdocs-material/"
                target="_blank" rel="noopener">
                Material for MkDocs
              </a>
              制作 (Made with)

            </div>

            <div class="md-social">

              <a href="https://github.com/google/flatbuffers" target="_blank"
                rel="noopener" title="github.com" class="md-social__link">
                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 496 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                    d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8M97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2" /></svg>
              </a>

              <a href="https:///discord.gg/6qgKs3R" target="_blank"
                rel="noopener" title class="md-social__link">
                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 640 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                    d="M524.531 69.836a1.5 1.5 0 0 0-.764-.7A485 485 0 0 0 404.081 32.03a1.82 1.82 0 0 0-1.923.91 338 338 0 0 0-14.9 30.6 447.9 447.9 0 0 0-134.426 0 310 310 0 0 0-15.135-30.6 1.89 1.89 0 0 0-1.924-.91 483.7 483.7 0 0 0-119.688 37.107 1.7 1.7 0 0 0-.788.676C39.068 183.651 18.186 294.69 28.43 404.354a2.02 2.02 0 0 0 .765 1.375 487.7 487.7 0 0 0 146.825 74.189 1.9 1.9 0 0 0 2.063-.676A348 348 0 0 0 208.12 430.4a1.86 1.86 0 0 0-1.019-2.588 321 321 0 0 1-45.868-21.853 1.885 1.885 0 0 1-.185-3.126 251 251 0 0 0 9.109-7.137 1.82 1.82 0 0 1 1.9-.256c96.229 43.917 200.41 43.917 295.5 0a1.81 1.81 0 0 1 1.924.233 235 235 0 0 0 9.132 7.16 1.884 1.884 0 0 1-.162 3.126 301.4 301.4 0 0 1-45.89 21.83 1.875 1.875 0 0 0-1 2.611 391 391 0 0 0 30.014 48.815 1.86 1.86 0 0 0 2.063.7A486 486 0 0 0 610.7 405.729a1.88 1.88 0 0 0 .765-1.352c12.264-126.783-20.532-236.912-86.934-334.541M222.491 337.58c-28.972 0-52.844-26.587-52.844-59.239s23.409-59.241 52.844-59.241c29.665 0 53.306 26.82 52.843 59.239 0 32.654-23.41 59.241-52.843 59.241m195.38 0c-28.971 0-52.843-26.587-52.843-59.239s23.409-59.241 52.843-59.241c29.667 0 53.307 26.82 52.844 59.239 0 32.654-23.177 59.241-52.844 59.241" /></svg>
              </a>

              <a href="https://twitter.com/dbaileychess" target="_blank"
                rel="noopener" title="twitter.com" class="md-social__link">
                <svg xmlns="http://www.w3.org/2000/svg"
                  viewBox="0 0 512 512"><!--! Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2024 Fonticons, Inc.--><path
                    d="M389.2 48h70.6L305.6 224.2 487 464H345L233.7 318.6 106.5 464H35.8l164.9-188.5L26.8 48h145.6l100.5 132.9zm-24.8 373.8h39.1L151.1 88h-42z" /></svg>
              </a>

            </div>

          </div>
        </div>
      </footer>

    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>

    <script id="__config"
      type="application/json">{"base": "../..", "features": ["content.code.annotate", "content.tabs.link", "navigation.expand", "navigation.footer", "header.autohide", "content.action.edit"], "search": "../../assets/javascripts/workers/search.f8cc74c7.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>

    <script src="../../assets/javascripts/bundle.c8b220af.min.js"></script>

  </body>

  <!-- Mirrored from flatbuffers.dev/languages/c_sharp/ by HTTrack Website Copier/3.x [XR&CO'2014], Thu, 08 May 2025 07:01:35 GMT -->
</html>